package com.atom.wyz.new_opengl;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public abstract class AbstractGLSurfaceView extends GLSurfaceView {
    protected NewOpenGLActivity activity ;

    public AbstractGLSurfaceView(Context context) {
        super(context);
        activity = (NewOpenGLActivity) context;
        this.setEGLContextClientVersion(3);
        this.setRenderer(getRenderer());
        this.setRenderMode(getMode());

    }

    protected int getMode(){
       return  GLSurfaceView.RENDERMODE_CONTINUOUSLY ;
    }

    protected abstract Renderer getRenderer();

    public AbstractGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public static abstract class AbstractRenderer implements Renderer{
        public float ratio ;
        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
            create(gl10 , eglConfig) ;
        }

        protected abstract void create(GL10 gl10, EGLConfig eglConfig);

        @Override
        public void onSurfaceChanged(GL10 gl10, int i, int i1) {
            GLES30.glViewport(0, 0, i, i1);
            ratio = (float) i / i1;
            changed(gl10 , i , i1 , ratio) ;
        }

        protected abstract void changed(GL10 gl10, int i, int i1, float ratio);

        @Override
        public void onDrawFrame(GL10 gl10) {
            GLES30.glClearColor(0, 0, 0, 0);
            GLES30.glEnable(GL10.GL_DEPTH_TEST);
            //GLES30.glEnable(GL10.GL_CULL_FACE);
            draw(gl10) ;
        }

        protected abstract void draw(GL10 gl10);
    }
}
